<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出境审批编号" prop="approvalNumber">
        <el-input
          v-model="queryParams.approvalNumber"
          placeholder="请输入出境审批编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组团单位" prop="groupUnits">
        <el-input
          v-model="queryParams.groupUnits"
          placeholder="请输入组团单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['document:overseas:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['document:overseas:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['document:overseas:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['document:overseas:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="overseasList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="人员类型" align="center" prop="typePerson" />
      <el-table-column label="开始日期" align="center" prop="beginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原因" align="center" prop="cause" />
      <el-table-column label="是否查看承诺书" align="center" prop="isSee" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="出生年月" align="center" prop="birthTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政治面貌" align="center" prop="politicalOutlook" />
      <el-table-column label="工作单位" align="center" prop="workplace" />
      <el-table-column label="出境类型" align="center" prop="typeExit" />
      <el-table-column label="职务" align="center" prop="office" />
      <el-table-column label="涉密程度" align="center" prop="degreeConfidentiality" />
      <el-table-column label="护照号码" align="center" prop="passportNumber" />
      <el-table-column label="出境目的地" align="center" prop="outboundDestination" />
      <el-table-column label="出境审批编号" align="center" prop="approvalNumber" />
      <el-table-column label="组团单位" align="center" prop="groupUnits" />
      <el-table-column label="前往国家" align="center" prop="travelingCountry" />
      <el-table-column label="停留日期" align="center" prop="dateStay" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.dateStay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出境情况" align="center" prop="groupOther" />
      <el-table-column label="境外到达日期" align="center" prop="arrivalDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.arrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="境外目的地点" align="center" prop="destinations" />
      <el-table-column label="境外事项" align="center" prop="matters" />
      <el-table-column label="附件" align="center" prop="annex" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['document:overseas:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['document:overseas:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出境申请对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="overseasRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="人员类型" prop="typePerson">
          <el-input v-model="form.typePerson" placeholder="请输入人员类型" />
        </el-form-item>
        <el-form-item label="开始日期" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原因">
          <editor v-model="form.cause" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否查看承诺书" prop="isSee">
          <el-input v-model="form.isSee" placeholder="请输入是否查看承诺书" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthTime">
          <el-date-picker clearable
            v-model="form.birthTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalOutlook">
          <el-input v-model="form.politicalOutlook" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="工作单位" prop="workplace">
          <el-input v-model="form.workplace" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="出境类型" prop="typeExit">
          <el-input v-model="form.typeExit" placeholder="请输入出境类型" />
        </el-form-item>
        <el-form-item label="职务" prop="office">
          <el-input v-model="form.office" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="涉密程度" prop="degreeConfidentiality">
          <el-input v-model="form.degreeConfidentiality" placeholder="请输入涉密程度" />
        </el-form-item>
        <el-form-item label="护照号码" prop="passportNumber">
          <el-input v-model="form.passportNumber" placeholder="请输入护照号码" />
        </el-form-item>
        <el-form-item label="出境目的地" prop="outboundDestination">
          <el-input v-model="form.outboundDestination" placeholder="请输入出境目的地" />
        </el-form-item>
        <el-form-item label="出境审批编号" prop="approvalNumber">
          <el-input v-model="form.approvalNumber" placeholder="请输入出境审批编号" />
        </el-form-item>
        <el-form-item label="组团单位" prop="groupUnits">
          <el-input v-model="form.groupUnits" placeholder="请输入组团单位" />
        </el-form-item>
        <el-form-item label="前往国家" prop="travelingCountry">
          <el-input v-model="form.travelingCountry" placeholder="请输入前往国家" />
        </el-form-item>
        <el-form-item label="停留日期" prop="dateStay">
          <el-date-picker clearable
            v-model="form.dateStay"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择停留日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出境情况" prop="groupOther">
          <el-input v-model="form.groupOther" placeholder="请输入出境情况" />
        </el-form-item>
        <el-form-item label="境外到达日期" prop="arrivalDate">
          <el-date-picker clearable
            v-model="form.arrivalDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择境外到达日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="境外目的地点" prop="destinations">
          <el-input v-model="form.destinations" placeholder="请输入境外目的地点" />
        </el-form-item>
        <el-form-item label="境外事项">
          <editor v-model="form.matters" :min-height="192"/>
        </el-form-item>
        <el-form-item label="附件" prop="annex">
          <file-upload v-model="form.annex"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker clearable
            v-model="form.creatTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Overseas">
import { listOverseas, getOverseas, delOverseas, addOverseas, updateOverseas } from "@/api/document/overseas";

const { proxy } = getCurrentInstance();

const overseasList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    approvalNumber: null,
    groupUnits: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询出境申请列表 */
function getList() {
  loading.value = true;
  listOverseas(queryParams.value).then(response => {
    overseasList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    typePerson: null,
    beginTime: null,
    endTime: null,
    cause: null,
    isSee: null,
    gender: null,
    birthTime: null,
    politicalOutlook: null,
    workplace: null,
    typeExit: null,
    office: null,
    degreeConfidentiality: null,
    passportNumber: null,
    outboundDestination: null,
    approvalNumber: null,
    groupUnits: null,
    travelingCountry: null,
    dateStay: null,
    groupOther: null,
    arrivalDate: null,
    destinations: null,
    matters: null,
    annex: null,
    updateTime: null,
    creatTime: null
  };
  proxy.resetForm("overseasRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加出境申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOverseas(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改出境申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["overseasRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOverseas(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOverseas(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除出境申请编号为"' + _ids + '"的数据项？').then(function() {
    return delOverseas(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('document/overseas/export', {
    ...queryParams.value
  }, `overseas_${new Date().getTime()}.xlsx`)
}

getList();
</script>
